var messageList = null;
var messageType;

$(document).ready(function(){
	showMessageList(1);
	
	addFadeEffect();
});

function showMessageList(type) {
	var requestUrl = "./get_messages";
	var params = {
		type:type,
		page:1,
		page_size:999
	};
	
	$.post(requestUrl, params, function(data){
		var newHtml = "";
		
		if (data.resultData && data.resultData.items) {
			messageList = data.resultData.items;
			messageType = type;
		
			for (var i = 0; i < data.resultData.items.length; i++){
				var item = data.resultData.items[i];
				
				newHtml += '<div align="left" style="height: 68px; padding-left: 15px; font-size: 13pt; border: 1px solid gray; background: url(../image/bg_request_list.png); cursor: pointer;" onclick="showMessageDetail(' + i + ')">';
				newHtml += '	<div style="padding-top: 15px;">';
				newHtml += item.customerId + " " + item.date;
				newHtml +='		</div>';
				newHtml += '	<div>' + item.title + '</div>';
				newHtml += '</div>';
			}
			
		} else {
			messageList = null;
		}
		
		$("#div_message_list").html(newHtml);
		changeImage(type);

		showMessageDetail(0);
		
		addFadeEffect();
	}, "json");
}

function showMessageDetail(index) {
	var newHtml = "";
	
	if (messageList != null) {
	var message = messageList[index];
	
		newHtml += '<div style="height: 65px; border-bottom: 1px dashed gray;">';
		newHtml += '	<div style="padding-left: 25px; padding-top: 15px">';
		newHtml += '		<b style="font-size: 13pt;">' + message.title + '</b><br/>';
		newHtml += '		<b>' + message.date + '</b>';
		newHtml += '	</div>';
		newHtml += '</div>';
		newHtml += '<div style="min-height: 200px; margin-left: 10px;">';
		newHtml +=  message.title;
		newHtml += '</div>';
		newHtml += '<div align="center" style="margin-top: 15px;">';
		newHtml += '	<img class="pointer fade_effect" src="../image/btn_delete2.png" onclick="deleteMessage(\'' + message.messageId + '\')"/>';
		newHtml += '</div>';
	}
	
	$("#div_message_detail").html(newHtml);
}

function deleteMessage(messageId) {
	var requestUrl = "./delete_message";
	var params = {
		type:messageType,
		message_id:messageId
	};
	
	$.post(requestUrl, params, function(ret){
		if (ret.code == 0) {
			showMessageList(messageType);
		}
	}, "json");
}

function changeImage(type) {
	if (type == 1) {
		$("#img_receive").attr("src", "../image/btn_receive_box_on.png");
		$("#img_send").attr("src", "../image/btn_send_box_off.png");
	} else {
		$("#img_receive").attr("src", "../image/btn_receive_box_off.png");
		$("#img_send").attr("src", "../image/btn_send_box_on.png");
	}
}